package managedbean.PurchaseOrder; 
 
import entity.LineItem;
import entity.PurchaseOrder;
import javax.faces.event.ActionEvent;
import java.io.IOException;
import java.util.List; 
import javax.ejb.EJB; 
import javax.faces.bean.ManagedBean; 
import javax.faces.bean.ViewScoped; 
import javax.faces.context.FacesContext;
import session.stateless.PurchaseOrderBeanLocal; 
 
@ManagedBean(name="viewPurchaseOrder")
@ViewScoped 
public class ViewPurchaseOrderManagerBean { 
    @EJB 
    private PurchaseOrderBeanLocal purchaseOrderBeanLocal; 
    
    private PurchaseOrder selectedPurchaseOrder;
    private List<LineItem> lineItems;
    
    public ViewPurchaseOrderManagerBean() { 
    } 
 
 
    public List<PurchaseOrder> getPurchaseOrders() 
    { 
        return purchaseOrderBeanLocal.getPurchaseOrders(); 
    } 
    
    public PurchaseOrder getSelectedPurchaseOrder() {
        return selectedPurchaseOrder;
    }

    public void setSelectedPurchaseOrder(PurchaseOrder selectedPurchaseOrder) {
        this.selectedPurchaseOrder = selectedPurchaseOrder;
    }

    public List<LineItem> getLineItems() {
        return lineItems;
    }

    public void setLineItems(List<LineItem> lineItems) {
        this.lineItems = lineItems;
    }

    public PurchaseOrderBeanLocal getPurchaseOrderBeanLocal() {
        return purchaseOrderBeanLocal;
    }

    public void setPurchaseOrderBeanLocal(PurchaseOrderBeanLocal purchaseOrderBeanLocal) {
        this.purchaseOrderBeanLocal = purchaseOrderBeanLocal;
    }

        
    public void editPurchaseOrder(ActionEvent event) throws IOException
    {
        PurchaseOrder selectedPurchaseOrder = this.selectedPurchaseOrder;
        if(selectedPurchaseOrder != null)
        {
            FacesContext.getCurrentInstance().getExternalContext().getFlash().put("selectedPurchaseOrder", selectedPurchaseOrder);
            FacesContext.getCurrentInstance().getExternalContext().redirect("editPurchaseOrder.xhtml");      
        }
    }
    
    public void deletePurchaseOrder(ActionEvent event) throws IOException
    {
       PurchaseOrder selectedPurchaseOrder = this.getSelectedPurchaseOrder();

        try
        {
            purchaseOrderBeanLocal.deletePurchaseOrder(selectedPurchaseOrder.getId());
            FacesContext.getCurrentInstance().getExternalContext().redirect("viewPurchaseOrder.xhtml");
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }

    
} 